﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.ch30.d08 {
    class HelloGeneric8 {
        public static void Demo() {
            Test1();
        }
        public static void Test1() {
            int[] a1 = { 1, 2, 4, 5 };
            int[] a2 = { 1, 2, 3, 4, 5, 6 };
            double[] a3 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
            double[] a4 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
            var result1 = Zip(a1, a2);
            var result2 = Zip(a3, a4);
            Console.WriteLine(string.Join(", ", result1));
            Console.WriteLine(string.Join(", ", result2));
        }


        static T[] Zip<T>(T[] a, T[] b) {
            T[] zipped = new T[a.Length + b.Length];
            int aIndex = 0, bIndex = 0, zIndex = 0;
            do {
                if (aIndex < a.Length) zipped[zIndex++] = a[aIndex++];
                if (bIndex < b.Length) zipped[zIndex++] = b[bIndex++];
            } while (aIndex < a.Length || bIndex < b.Length);

            return zipped;
        }
    }
}
